package com.ddm.controller.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ddm.entity.webEntity.Course;
import com.ddm.result.PageResult;
import com.ddm.result.Result;
import com.ddm.service.webService.CourseService;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController("WebCourseController")
@RequestMapping("/Web/course")
@Api(tags = "课程相关接口")
@Slf4j
public class CourseController {

    @Autowired
    private CourseService courseService;



    @GetMapping("/findById")
    @ApiOperation("根据id查询课程")
    public Result<Course> getById( @ApiParam(value = "id", required = true)
                                       @RequestParam(required = false) Integer id) {
        log.info("根据id查询课程:{}", id);
        Course course = courseService.getCourseById(id);
        return Result.success(course);
    }



    @GetMapping("/findByType")
    @ApiOperation("分页查询所有课程信息")
    @JsonFormat(shape = JsonFormat.Shape.STRING)
    public Result<PageResult> getCoursesByType(
            @ApiParam(value = "当前页码", required = false, defaultValue = "1")
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @ApiParam(value = "每页记录数", required = false, defaultValue = "10")
            @RequestParam(required = false, defaultValue = "10") Integer pageSize,
            @ApiParam(value = "关键词（模糊查询）", required = false)
            @RequestParam(required = false) String type

    ) {
        Page<Course> page = new Page<>(pageNum, pageSize);
        IPage<Course> coursePage = courseService.getCoursesByType(page,type);

        // 使用 PageResult 封装分页信息
        PageResult pageResult = new PageResult(
                coursePage.getTotal(),      // 总记录数
                coursePage.getRecords()     // 当前页数据
        );

        return Result.success(pageResult);
    }


    @PostMapping
    @ApiOperation("添加课程")
    public Result<Course> add(@RequestBody @Valid Course course) {
        Boolean result = courseService.add(course);
        // 显式指定泛型类型
        return result ? Result.success(course) : Result.error("添加失败");
    }

    @PutMapping
    @ApiOperation("更新课程")
    public Result<Course> update(@RequestBody Course course) {
        courseService.update(course);
        return Result.success(course);
    }


    @DeleteMapping("/delete")
    @ApiOperation("删除课程")
    public Result<Course> delete(@ApiParam(value = "id", required = true)
                                      @RequestParam(required = false) Integer id) {
        Course course= courseService.getCourseById(id);
        Boolean result = courseService.delete(id);
        return Result.success(course);
    }


    @GetMapping("/pageAll")
    @ApiOperation("分页查询所有课程信息")
    @JsonFormat(shape = JsonFormat.Shape.STRING)
    public Result<PageResult> getAllCoursesByPage(
            @ApiParam(value = "当前页码", required = false, defaultValue = "1")
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @ApiParam(value = "每页记录数", required = false, defaultValue = "10")
            @RequestParam(required = false, defaultValue = "10") Integer pageSize,
            @ApiParam(value = "关键词（模糊查询）", required = false)
            @RequestParam(required = false) String key

    ) {
        Page<Course> page = new Page<>(pageNum, pageSize);
        IPage<Course> coursePage = courseService.getCoursesByPage(page, key);

        // 使用 PageResult 封装分页信息
        PageResult pageResult = new PageResult(
                coursePage.getTotal(),      // 总记录数
                coursePage.getRecords()     // 当前页数据
        );

        return Result.success(pageResult);
    }

    /**
     //     * 分页查询沉
     //     *根据用户收藏数据查询
     模糊查询 课程名 课程作者
     //     * @return 培训课程列表
     */

    @GetMapping("/pageByUser")
    @ApiOperation("分页查询用户收藏课程信息")
    @JsonFormat(shape = JsonFormat.Shape.STRING)
    public Result<PageResult> getCoursesByPageAndUser(
            //添加用户id
            @ApiParam(value = "用户id", defaultValue = "1")
            @RequestParam(required = true) Integer userId,
            @ApiParam(value = "当前页码", required = false, defaultValue = "1")
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @ApiParam(value = "每页记录数", required = false, defaultValue = "10")
            @RequestParam(required = false, defaultValue = "10") Integer pageSize,
            @ApiParam(value = "关键词（模糊查询）", required = false)
            @RequestParam(required = false) String key)
    {
        Page<Course> page = new Page<>(pageNum, pageSize);
        // 传入 userId 和 key
        IPage<Course> coursePage = courseService.getCollectedCoursesByUserId(page, userId, key);

        // 使用 PageResult 封装分页信息
        PageResult pageResult = new PageResult(
                coursePage.getTotal(),      // 总记录数
                coursePage.getRecords()     // 当前页数据
        );

        return Result.success(pageResult);
    }
}